#include "ThreadPoolTest.h"
#include <vector>
#include <Windows.h>

void ThreadPoolTest::PrintResult(int a, int b)
{
	printf("%d + %d = %d.\n", a, b, a + b);
	printf("%d - %d = %d.\n", a, b, a - b);
	printf("%d * %d = %d.\n", a, b, a * b);
	printf("%d / %d = %d.\n", a, b, a / b);
}

int ThreadPoolTest::Multiply(int a, int b)
{
	return a * b;
}

int ThreadPoolTest::Sum(int a, int b)
{
	return a + b;
}

ThreadPoolTest::ThreadPoolTest(int num)
{
	executor = new threadlocal::ThreadPool(num);
}

ThreadPoolTest::~ThreadPoolTest()
{
	if (executor)
	{
		delete executor;
		executor = nullptr;
	}
}

void ThreadPoolTest::Start()
{
	// example with lambda.
	//auto task = [](int a, int b, int delay)
	//{
	//	auto tid = std::this_thread::get_id();
	//	printf("Execute ((%d + %d) * (%d - %d) = %d) at Thread [%d] after [%d] ms\n",
	//		a, b, a, b, (a + b) * (a - b), *((unsigned int*)&tid), delay * 1000);
	//	std::this_thread::sleep_for(std::chrono::milliseconds(delay * 1000));
	//	return (a + b) * (a - b);
	//};
	//executor->execute(task, 3, 3);

	for (int i = 0; i < 10; i++)
	{
		executor->execute(&ThreadPoolTest::PrintResult, this, 10 + i, 10 - i);
	}
	Sleep(100);
	std::vector<std::future<int>> results;
	for (int i = 0; i < 50; i++)
	{
		std::future<int> t = executor->submit(&ThreadPoolTest::Sum, this, 5 + i, 5 * i);
		results.push_back(std::move(t));
	}
	Sleep(100);
	for (auto&& r : results)
	{
		printf("Task result: [%d]\n", r.get());
	}
}

void ThreadPoolTest::Stop()
{

}
